<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Home page
 *
 * @category	Home
 */
class IndexAction extends HomeAction
{


	function _initialize()
	{
		parent::_initialize();
	}


	public function index()
    {
    	
        $htmlFile = HTML_PATH . 'homepage.html';
        $htmlExpiration = intval($this->_global['cache_home_page_expiration'] * 60);
        
        /*-------------------- 给游客加载静态缓存 ---------------------*/
        if (6 == $this->_user['user_group'] && $htmlExpiration > 0)
        {
        	if (file_exists($htmlFile) && filemtime($htmlFile) + $htmlExpiration > time())
    		{
    			readfile($htmlFile);
    			exit;
    		}
        }

    	//公告
    	$announcements = array();
    	$group = $this->_userGroup['group_id'];
    	$tmp = F('cache_announcements');
    	if (!empty($tmp))
    	{
    		foreach ($tmp as $value)
    		{
    		    $targetGroups = explode(',', $value['target_group']);
                if (!empty($targetGroups) && in_array($group, $targetGroups))
                {
                	$announcements[] = $value;
                }
    		}
    	}

    	//最新问题
    	$View = D('QuestionView');
    	$where = array(
    		'status' => 1,
    		'is_visible' => 1,
    	);
    	$limit = 15;
    	$order = array('question_id' => 'desc');
    	$questions = $View->where($where)->order($order)->limit($limit)->select();

    	//在线会员
    	$Online = M('UserSession');
    	$limit = 100;
    	$map = $onlineCount = $onlineUsers = array();

    	$onlineCount['total'] = $Online->count();
    	$onlineCount['guest'] = $Online->where(array('user_group' => 6))->count();
    	$onlineCount['registered'] = $onlineCount['total'] - $onlineCount['guest'];
    	$onlineCount['hidden'] = $Online->where(array('invisible' => 1))->count();
    	$map = array('invisible' => 0);
    	$onlineUsers = $Online->where($map)->limit($limit)->select();

    	$online = array(
    	   'total_count' => sprintf(L('online_total_count'), $onlineCount['total']),
    	   'registered_count' => sprintf(L('online_registered_count'), $onlineCount['registered']),
    	   'hidden_count' => sprintf(L('online_hidden_count'), $onlineCount['hidden']),
    	   'guest_count' => sprintf(L('online_guest_count'), $onlineCount['guest']),
    	   'explain' => sprintf(L('online_explain'), $this->_global['online_hold_time']),
    	   'users' => $onlineUsers,
    	);

    	$this->assign('announcements', $announcements);
    	$this->assign('questions', $questions);
    	$this->assign('online', $online);

    	/*----------------  给游客生成静态页面 ------------------*/
    	if (6 == $this->_user['user_group'] && $htmlExpiration > 0)
        {
        	if (!file_exists($htmlFile) || filemtime($htmlFile) + $htmlExpiration < time())
    		{
    			$html = $this->fetch();
    			file_put_contents($htmlFile, $html);
    		}
        }

    	$this->display();
    }

}


/* End of file IndexAction.class.php */
/* Location: ./App/Lib/Action/Home/IndexAction.class.php */